//
//  DeviceAuthorizationUtil.swift
//  vr_web
//
//  Created by 李旭峰 on 2020/7/13.
//

import UIKit
import AVFoundation
import AVKit

class DeviceAuthorizationUtil: NSObject {
    static func audioAuthorizationStatus(_ response: (AVAuthorizationStatus) -> Void){
        let mediaType = AVMediaType.audio
        let authorizationStatus = AVCaptureDevice.authorizationStatus(for: mediaType)
        response(authorizationStatus)
    }
    static func requestAudioRecordPermission(_ response: @escaping (_ result:Bool)->Void){
        AVAudioSession.sharedInstance().requestRecordPermission { (granted) in
            response(granted)
        }
    }
    static func audioAuthorization(_ success:@escaping ()->Void,_ fail:@escaping (_ isFirst:Bool)->Void){
        self.audioAuthorizationStatus { (status) in
            switch status {
            case .notDetermined:
                self.requestAudioRecordPermission { (granted) in
                    if granted {
                        success()
                    }else {
                        fail(true)
                    }
                }
            case .authorized:success()
            default:fail(true)
            }
        }
    }
}
